﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Kaskelot.Services.Model;

namespace Stockholm.SOA.Kaskelot.WCF.ServiceImplementation.Mapping
{
	public static class ValkretsExtensions
	{
		public static DataContracts.Valkrets ToContract(this Valkrets entity)
		{
			DataContracts.Valkrets b = new DataContracts.Valkrets();

			CopyFromEntity(entity, b, false);

			return b;
		}

		public static DataContracts.Valkrets ToMinimalContract(this Valkrets entity)
		{
			DataContracts.Valkrets b = new DataContracts.Valkrets();

			CopyFromEntity(entity, b, true);

			return b;
		}

		public static Valkrets ToEntity(this DataContracts.Valkrets b)
		{
			Valkrets entity = new Valkrets();

			CopyToEntity(b, entity);

			return entity;
		}

		public static void UpdateEntity(this Valkrets entity, DataContracts.Valkrets valkrets)
		{
			CopyToEntity(valkrets, entity);
		}

		private static void CopyFromEntity(Valkrets from, DataContracts.Valkrets to, bool minimal)
		{
			to.Id = from.Id;
			to.ValId = from.ValId;
			to.Namn = from.Benamning;

			to.Andrad = from.Andrad;
			to.AndradAv = from.AndradAv;
			to.Skapad = from.Skapad;
			to.SkapadAv = from.SkapadAv;

			to.ValkretsNr = from.ValkretsNr;

			if (minimal) return;
			to.Val = from.Val.ToMinimalContract();

			if (from.Forsamling != null)
			{
				to.Forsamlingar = new List<DataContracts.Forsamling>();
				foreach (Forsamling f in from.Forsamling)
				{
					to.Forsamlingar.Add(f.ToMinimalContract());
				}
			}
		}

		private static void CopyToEntity(DataContracts.Valkrets from, Valkrets to)
		{
			to.Id = from.Id;
			to.ValId = from.ValId;
			to.Benamning = from.Namn;

			to.Andrad = from.Andrad;
			to.AndradAv = from.AndradAv;
			to.Skapad = from.Skapad;
			to.SkapadAv = from.SkapadAv;

			to.ValkretsNr = from.ValkretsNr;
		}
	}
}
